Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Health Checks #36

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

Add Health Checks #36

wants to merge 12 commits into from

Conversation

bradp
Copy link
Contributor

@bradp bradp commented Dec 11, 2024

Proposed changes

Implements a variety of Health Checks to the Site Health page.

Screenshot 2024-12-10 at 1 24 01 PM

Jira tickets: PRESS7-108, PRESS7-109, PRESS7-110, PRESS7-111, PRESS7-118, PRESS7-112, PRESS7-113, PRESS7-121, PRESS7-107, PRESS7-119, PRESS7-120, PRESS7-114, PRESS7-115, PRESS7-116, PRESS7-117.

Type of Change

  • New feature (non-breaking change which adds functionality)

Screenshot

Screenshot 2024-12-10 at 1 23 43 PM

Checklist

  • I have read the CONTRIBUTING doc
  • I have viewed my change in a web-browser
  • Linting and tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

This PR adds HealthCheckManager.php which is a helper class to set up and add health checks to the Site Health page. This is used in HealthChecks.php to add the health checks to the Site Health page.

This makes it so adding a new health check is as simple as:

$manager->addHealthCheck( [
  'id'    => 'example-health-check',
  'title' => __( 'Example Health Check', 'newfold-performance-module' ),
  'pass'  => __( 'Example health check passed', 'newfold-performance-module' ),
  'fail'  => __( 'Example health check failed', 'newfold-performance-module' ),
  'text'  => __( 'This is some text to explain the health check to the user.', 'newfold-performance-module' ),
  'test'  => function () {
    // Example test for a constant being defined and having a specific value.
    return ( defined( 'EXAMPLE_CONSTANT' ) && EXAMPLE_CONSTANT === 'example-value' );
  },
] );

This PR also adds Health Checks that are commented out for the following PRs

@bradp bradp added the enhancement New feature or request label Dec 11, 2024
@bradp bradp changed the title Feature/health checks Add Health Checks Dec 11, 2024
Copy link
Member

@arunshenoy99 arunshenoy99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few minor things, @bradp. Everything else looks good to me! 😃

/**
* Add performance health checks.
*/
class HealthCheckManager {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we move the health checks related files to a HealthChecks directory? It will help keep the related code better organized.

*
* @var Container
*/
protected $container;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need $container? I don't see it being used anywhere in the class.

*
* @param array $options Health check options.
*/
public function addHealthCheck( $options ) {
Copy link
Member

@arunshenoy99 arunshenoy99 Dec 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's stick to using snake casing for function names, as per WordPress standards.

*/
public function addHealthCheck( $options ) {
$options = wp_parse_args(
$options,
Copy link
Member

@arunshenoy99 arunshenoy99 Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add some documentation to explain what these keys represent and how the values affect the functionality

$manager->addHealthCheck(
array(
'id' => 'autosave-interval',
'title' => __( 'Autosave Interval', 'newfold-performance-module' ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'title' => __( 'Autosave Interval', 'newfold-performance-module' ),
'title' => __( 'Autosave Interval', 'newfold-module-performance' ),

I think this is the right namespace we're using for the i18n function.

/**
* Add health checks.
*/
public function addHealthChecks() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this works well for now. But if this file grows into a large monolith in the future, do you think it might make sense to create an abstract class for new health checks to inherit from?

For instance, we could have a HealthChecks directory with an AbstractHealthCheck.php file that defines and documents all the necessary fields. A class like LinkPrefetchHealthCheck.php could then inherit from it and include all tests related to link prefetching.

In this file, we can simply call $manager->addHealthCheck( new LinkPrefetchHealthCheck() );. This approach would provide better validation and typing in PHP, prevent files from becoming too large, group related functionality, and avoid people adding random data directly into the manager. What do you think?


/* // phpcs:ignore Squiz.PHP.CommentedOutCode
// Enable when https://github.com/newfold-labs/wp-module-performance/pull/26 is merged.
// PRESS7-120: Link Prefetching.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Link Prefetch and Jetpack Boost stuff has been merged. I'm working on getting the Image Optimization changes merged as soon as possible after a review! 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants